package cn.youyeku.zsxh.ui.practice;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.ConverUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.api.XhApi;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


/**
 * 最后一个fgm 提交数据
 */

public class ScantronItemFragment extends Fragment {
    LocalBroadcastManager mLocalBroadcastManager;
    NoScrollGridView gv;
    MyAdapter adapter;
    TextView tv_submit_result;
    private TextView mTvPaperName;

    public ScantronItemFragment() {

    }

    int count = QuestionPracticeActivity.questions.size();
    int[] mIds = new int[count];

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initData();
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
        View rootView = inflater.inflate(R.layout.pager_item_scantron,
                container, false);
        gv = (NoScrollGridView) rootView.findViewById(R.id.gridview);
        tv_submit_result = (TextView) rootView.findViewById(R.id.tv_submit_result);
        tv_submit_result.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                submit_to_server();


            }
        });
        adapter = new MyAdapter(getActivity());
        gv.setAdapter(adapter);

        gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO跳转到相应的viewpager 页面
                Intent intent = new Intent("youyeku.jumptopage");
                intent.putExtra("index", position);
                mLocalBroadcastManager.sendBroadcast(intent);

            }
        });
        initView(rootView);
        return rootView;

    }

    private void submit_to_server() {
        if (QuestionPracticeActivity.submit) {
            return;
        }

        int len = QuestionPracticeActivity.do_questions.length;
        for (int pos = 0; pos < len; pos++) {
            if (QuestionPracticeActivity.do_questions[pos] == 0) {
                Toast.makeText(getContext(), "你还有题目没有做完", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        QuestionPracticeActivity.submit = true;
        tv_submit_result.setText("已经完成提交 点击上方红色选项查看错题");
        update();
        Intent intent = new Intent("youyeku.updatefgm");

        mLocalBroadcastManager.sendBroadcast(intent);

        String data = ConverUtils.SparseBooleanArrayToString(QuestionPracticeActivity.check_question);
        LogUtils.w(ConverUtils.SparseBooleanArrayToString(QuestionPracticeActivity.check_question));

        XhApi.post_question_count(data, QuestionPracticeActivity.question_id, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {


            }
        });
    }

    private void initData() {
        for (int i = 0; i < count; i++) {
            mIds[i] = i + 1;
        }
    }

    private void initView(View rootView) {
        mTvPaperName = (TextView) rootView.findViewById(R.id.tv_paper_name);
        mTvPaperName.setText(QuestionPracticeActivity.question_title);
    }

    private class MyAdapter extends BaseAdapter {
        private Context mContext;

        public MyAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public int getCount() {
            return mIds.length;
        }

        @Override
        public Object getItem(int position) {
            return mIds[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(mContext);
            tv.setGravity(Gravity.CENTER);
            tv.setLayoutParams(new GridView.LayoutParams(70, 70));
            tv.setPadding(8, 8, 8, 8);

            tv.setText(mIds[position] + "");


            if (QuestionPracticeActivity.submit) {
                if (QuestionPracticeActivity.check_question.get(position)) {
                    tv.setBackgroundResource(R.drawable.opt_round_selected);
                } else {
                    tv.setBackgroundResource(R.drawable.opt_round_selected_error);
                }

            } else {
                if (QuestionPracticeActivity.do_questions[position] == 1) {
                    tv.setBackgroundResource(R.drawable.opt_round_selected);
                } else {
                    tv.setBackgroundResource(R.drawable.opt_round_not_selected);
                }
            }


            return tv;
        }

    }

    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
    }

    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);
    }


    @Override
    public void onPause() {
        LogUtils.w("离开页面");
        super.onPause();
    }

    //更新界面
    public void update() {
        adapter.notifyDataSetChanged();

    }
}